package com.hex.ds.hdrs.app.service.impl;

import com.hex.ds.hdrs.app.dto.AppAdminRelationDto;
import com.hex.ds.hdrs.app.po.AppAdminRelationPo;
import com.hex.ds.hdrs.app.po.AppConf;
import com.hex.ds.hdrs.app.service.IAppAdminRelationService;
import com.hex.ds.hdrs.common.helper.EchartsHelper;
import com.hex.ds.hdrs.common.helper.RamHelper;
import com.hex.ds.hdrs.common.utils.BaseUtil;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import com.hex.ds.hdrs.log.po.RunningStateLog;
import com.hex.ds.hdrs.log.service.RunningStateLogService;
import com.hex.ds.hdrs.period.job.service.PeriodJobService;
import com.hex.ds.hdrs.period.task.service.PeriodTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author lwz
 * @date 2020-12-28
 * @des
 */
@Slf4j
@Service
public class AppMainService {

    @Autowired
    private AppConfService appConfService;
    @Autowired
    private RunningStateLogService runningStateLogService;
    @Autowired
    private PeriodTaskService periodTaskService;
    @Autowired
    private PeriodJobService periodJobService;
    @Autowired
    private IAppAdminRelationService appAdminRelationService;

    /**
     * 获取当前用户周期作业执行情况
     *
     * @return
     */
    public List findRunningInfByCurrentUser(String bizDate) {
        log.info("【RamServic.findRunningInfByCurrentUser】");
        List resultList = null;
        try {
            List<AppConf> appConfList = this.findAppByCurrentUser();
            resultList = new ArrayList();
            for (AppConf appConf : appConfList) {
                Map map = this.buildCycleJobRunningInf(appConf, bizDate);
                resultList.add(map);
            }
        } catch (Exception e) {
            log.error("获取当前用户周期作业执行情况出错: {}", e.toString(), e);
            throw e;
        }
        return resultList;
    }

    private Map buildCycleJobRunningInf(AppConf appConf, String bizDate) {
        Map map = new HashMap<>();
        map.put("appCode", appConf.getAppCode());
        map.put("appName", appConf.getAppName());
        map.put("todo", RamHelper.getCycleJobTodo(appConf.getAppCode(), bizDate));
        map.put("toRun", RamHelper.getCycleJobToRun(appConf.getAppCode(), bizDate));
        map.put("running", RamHelper.getCycleJobRunning(appConf.getAppCode(), bizDate));
        List<Map> list = this.periodJobService.countGroupByStatus(appConf.getAppCode(), bizDate);
        map.put("finish", RamHelper.getCycleJobFinish(list));
        map.put("noFinish", RamHelper.getCycleJobNoFinish(list));
        return map;
    }

    /**
     * 获取当前用户所属的应用
     * @return
     */
    public List<AppConf> findAppByCurrentUser(){
        log.info("【AppMainService.findAppByCurrentUser】");
        List<AppConf> resultList = new ArrayList<>();
        String username = BaseUtil.getUserId();
        resultList = this.appConfService.findAppByCurrentUser(username);
        AppAdminRelationDto appAdminRelationDto = new AppAdminRelationDto().setAdminUserCode(username);
        //1.查询应用用户关系表数据
        List<AppAdminRelationPo> list = this.appAdminRelationService.queryList(appAdminRelationDto);
        if (!CollectionUtils.isEmpty(list)){
            for (AppAdminRelationPo app : list){
                resultList.add(this.appConfService.qryAppConfByAppCode(app.getAppCode()));
            }
        }
        return resultList;
    }

    /**
     * @Method: findAppByCurrentUserByAppStatus
     * @Param: [appConf]
     * @Return: java.util.List<com.hex.ds.hdrs.app.po.AppConf>
     * @Description:  获取当前用户所属应用
     * @Author: ji.tang
     * @Date: 2022/11/11 13:40
     * @Version: V1.0
     */
    public List<AppConf> findAppByCurrentUserByAppStatus(AppConf appConf){
        log.info("【AppMainService.findAppByCurrentUserByAppStatus】");
        Map map = new HashMap();
        map.put("userId",BaseUtil.getUserId());
        map.put("appStatus",appConf.getAppSatus());
        List<AppConf> resultList = this.appConfService.findAppConfListByCurrentUser(map);
        return resultList;
    }

    /**
     * 获取当前用户所关的运行日志
     * @param pageIndex
     * @return
     */
    public List<RunningStateLog> findRunningLogByCurrentUser(Integer pageIndex) {
        String username = BaseUtil.getUserId();
        return this.runningStateLogService.findRunningLogByUser(username,pageIndex);
    }

    /**
     * 当前用户所属应用任务执行情况
     * @return
     */
    public Map findTaskRunningLog(){
        String username = BaseUtil.getUserId();
        Date nowDate = new Date();
        //1、获取当前用户所关联的所有应用
        Map<String,String> appMap = this.getXAxisAppByCurrentUser();
        //2、获取最近30分钟时间
        List<String> timeList = EchartsHelper.getXAxisTime(nowDate,30,1,0,"HH:mm");
        //3.获取所有应用最近30分钟的运行日志
        String startTime = HdrsDateUtil.format(DateUtils.addMinutes(nowDate,-30),"yyyy-MM-dd HH:mm:ss");
        List taskList = this.periodTaskService.getTaskListByCruuentUser(username,startTime);
        return this.getResultMap(appMap,timeList,taskList);
    }

    /**
     * 获取首页各应用任务执行情况的应用
     * @return
     */
    public Map<String,String> getXAxisAppByCurrentUser() {
        List<AppConf> appList = this.findAppByCurrentUser();
        Map<String,String> appMap = new HashMap<>();
        for (AppConf appConf : appList){
            appMap.put(appConf.getAppCode(),appConf.getAppName());
        }
        return appMap;
    }

    public Map getResultMap(Map<String,String> appMap,List<String> timeList,List taskList){
        Map resultMap = new HashMap();
        List legend = new ArrayList();
        List seriesList = new ArrayList();
        for (String appCode : appMap.keySet()){
            String appName = appMap.get(appCode);
            legend.add(appName);
            List seriesData = EchartsHelper.getSeriesData(taskList,timeList,"app_code",appCode);
            Map seriesMap = EchartsHelper.getSeriesData(appName,seriesData,"line");
            seriesList.add(seriesMap);
        }
        resultMap.put("legend",legend);
        resultMap.put("times",timeList);
        resultMap.put("seriesList",seriesList);
        return resultMap;
    }
}
